package com.mission.schedule.activity

import android.app.Activity
import android.content.Intent
import android.graphics.Color
import android.os.Bundle
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.mission.schedule.R
import com.mission.schedule.constants.ShareFile
import com.mission.schedule.utils.SharedPrefUtil
import kotlinx.android.synthetic.main.repeat_zhengdian.*
import kotlinx.android.synthetic.main.repeat_zhengdian_header.view.*
import kotlinx.android.synthetic.main.repeat_zhengdian_item.view.*
import android.app.AlarmManager
import android.app.PendingIntent
import android.content.Context
import com.mission.schedule.cutimage.Log
import com.mission.schedule.receiver.AlarmBootReciver
import com.mission.schedule.receiver.MTimeReceiver
import com.mission.schedule.service.AlarmService
import java.util.*
import android.content.IntentFilter
import android.content.res.AssetFileDescriptor
import android.media.AudioManager
import android.media.MediaPlayer
import android.os.Vibrator
import com.mission.schedule.service.MTimeService


/**
 * 整点半点提醒
 */
class RepeatZhengDianActivity : Activity() {

    enum class ITEM_TYPE {
        HEADER, ITEM
    }

    var zhengdian_data = arrayListOf(arrayOf(""),
            arrayOf("06:00", "07:00", "08:00"),
            arrayOf("09:00", "10:00", "11:00"),
            arrayOf("12:00", "13:00", "14:00"),
            arrayOf("15:00", "16:00", "17:00"),
            arrayOf("18:00", "19:00", "20:00"),
            arrayOf("21:00", "22:00", "23:00"),
            arrayOf(""),
            arrayOf("06:30", "07:30", "08:30"),
            arrayOf("09:30", "10:30", "11:30"),
            arrayOf("12:30", "13:30", "14:30"),
            arrayOf("15:30", "16:30", "17:30"),
            arrayOf("18:30", "19:30", "20:30"),
            arrayOf("21:30", "22:30", "23:30"))

    var zhengdian_song = arrayListOf(arrayOf(""),
            arrayOf("6", "7", "8"),
            arrayOf("9", "10", "11"),
            arrayOf("12", "13", "14"),
            arrayOf("15", "16", "17"),
            arrayOf("18", "19", "20"),
            arrayOf("21", "22", "23"),
            arrayOf(""),
            arrayOf("6.5", "7.5", "8.5"),
            arrayOf("9.5", "10.5", "11.5"),
            arrayOf("12.5", "13.5", "14.5"),
            arrayOf("15.5", "16.5", "17.5"),
            arrayOf("18.5", "19.5", "20.5"),
            arrayOf("21.5", "22.5", "23.5"))

    var zhengdian_select = mutableMapOf<String, Boolean>()
    var bandian_select = mutableMapOf<String, Boolean>()
    lateinit var sharep: SharedPrefUtil

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.repeat_zhengdian)

        sharep = SharedPrefUtil(this@RepeatZhengDianActivity, ShareFile.USERFILE)
        val zhengdian = sharep.getString(this@RepeatZhengDianActivity, ShareFile.USERFILE,
                ShareFile.ZDTX, "")
        val bandian = sharep.getString(this@RepeatZhengDianActivity, ShareFile.USERFILE,
                ShareFile.BDTX, "")

        setSelect(zhengdian, bandian)

        recylerview.adapter = RzAdapter()
        recylerview.layoutManager = LinearLayoutManager(this@RepeatZhengDianActivity)

        top_ll_back.setOnClickListener {
            onBackPressed()
        }
    }


    private fun setSelect(zd: String, bd: String) {
        if (!zd.isEmpty()) {
            val data = zd.substring(0, zd.length - 1).split(",")
            data.map {
                zhengdian_select[it] = true
            }
        }

        if (!bd.isEmpty()) {
            val data = bd.substring(0, bd.length - 1).split(",")
            data.map {
                bandian_select[it] = true
            }
        }
    }

    override fun onBackPressed() {
        super.onBackPressed()
        if (zhengdian_select.size + bandian_select.size > 0) {
            var zd_data = ""
            var bd_data = ""
            zhengdian_select.map {
                zd_data += "${it.key},"
            }
            bandian_select.map {
                bd_data += "${it.key},"
            }

            sharep.putString(this@RepeatZhengDianActivity, ShareFile.USERFILE, ShareFile.ZDTX, zd_data)
            sharep.putString(this@RepeatZhengDianActivity, ShareFile.USERFILE, ShareFile.BDTX, bd_data)
            sharep.putString(this@RepeatZhengDianActivity, ShareFile.USERFILE, ShareFile.ISTX, "0")//0提醒
            var intent = Intent(this@RepeatZhengDianActivity, MTimeService::class.java)
            startService(intent)
        } else {
            sharep.putString(this@RepeatZhengDianActivity, ShareFile.USERFILE, ShareFile.ZDTX, "")
            sharep.putString(this@RepeatZhengDianActivity, ShareFile.USERFILE, ShareFile.BDTX, "")
            sharep.putString(this@RepeatZhengDianActivity, ShareFile.USERFILE, ShareFile.ISTX, "1")
        }
    }


    inner class RzAdapter : RecyclerView.Adapter<RecyclerView.ViewHolder>() {

        override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
            if (holder is ItemHolder) {
                if (position != 0 || position != 20) {
                    holder.itemView.zhengdian_item1.text = zhengdian_data[position][0]
                    holder.itemView.zhengdian_item2.text = zhengdian_data[position][1]
                    holder.itemView.zhengdian_item3.text = zhengdian_data[position][2]

                    holder.itemView.zhengdian_item2.tag = zhengdian_data[position][1]
                    holder.itemView.zhengdian_item1.tag = zhengdian_data[position][0]
                    holder.itemView.zhengdian_item3.tag = zhengdian_data[position][2]

                    if (position < 7) {
                        holder.itemView.zhengdian_item1.isChecked = zhengdian_select[zhengdian_song[position][0]] ?: false
                        holder.itemView.zhengdian_item2.isChecked = zhengdian_select[zhengdian_song[position][1]] ?: false
                        holder.itemView.zhengdian_item3.isChecked = zhengdian_select[zhengdian_song[position][2]] ?: false
                    } else {
                        holder.itemView.zhengdian_item1.isChecked = bandian_select[zhengdian_song[position][0]] ?: false
                        holder.itemView.zhengdian_item2.isChecked = bandian_select[zhengdian_song[position][1]] ?: false
                        holder.itemView.zhengdian_item3.isChecked = bandian_select[zhengdian_song[position][2]] ?: false
                    }

                    if (holder.itemView.zhengdian_item1.isChecked) {
                        holder.itemView.zhengdian_item1.setTextColor(Color.WHITE)
                        holder.itemView.zhengdian_item1.setTextAppearance(this@RepeatZhengDianActivity, R.style.text_style_bold)
                    } else {
                        holder.itemView.zhengdian_item1.setTextColor(resources.getColor(R.color.gongkai_txt))
                        holder.itemView.zhengdian_item1.setTextAppearance(this@RepeatZhengDianActivity, R.style.text_style_normal)
                    }

                    if (holder.itemView.zhengdian_item2.isChecked) {
                        holder.itemView.zhengdian_item2.setTextColor(Color.WHITE)
                        holder.itemView.zhengdian_item2.setTextAppearance(this@RepeatZhengDianActivity, R.style.text_style_bold)
                    } else {
                        holder.itemView.zhengdian_item2.setTextColor(resources.getColor(R.color.gongkai_txt))
                        holder.itemView.zhengdian_item2.setTextAppearance(this@RepeatZhengDianActivity, R.style.text_style_normal)
                    }

                    if (holder.itemView.zhengdian_item3.isChecked) {
                        holder.itemView.zhengdian_item3.setTextColor(Color.WHITE)
                        holder.itemView.zhengdian_item3.setTextAppearance(this@RepeatZhengDianActivity, R.style.text_style_bold)
                    } else {
                        holder.itemView.zhengdian_item3.setTextColor(resources.getColor(R.color.gongkai_txt))
                        holder.itemView.zhengdian_item3.setTextAppearance(this@RepeatZhengDianActivity, R.style.text_style_normal)
                    }

//                    holder.itemView.zhengdian_item1.setOnCheckedChangeListener(null)
//                    holder.itemView.zhengdian_item2.setOnCheckedChangeListener(null)
//                    holder.itemView.zhengdian_item3.setOnCheckedChangeListener(null)

                    holder.itemView.zhengdian_item1.setOnClickListener {
                        if (holder.itemView.zhengdian_item1.isChecked) {
                            holder.itemView.zhengdian_item1.setTextColor(Color.WHITE)
                            if (position < 7) {
                                zhengdian_select[zhengdian_song[position][0]] = true
                            } else {
                                bandian_select[zhengdian_song[position][0]] = true
                            }
                        } else {
                            holder.itemView.zhengdian_item1.setTextColor(resources.getColor(R.color.gongkai_txt))
                            if (position < 7) {
                                zhengdian_select.remove(zhengdian_song[position][0])
                            } else {
                                bandian_select.remove(zhengdian_song[position][0])
                            }
                        }
                        notifyItemChanged(position, 1)
                        notifyItemChanged(0, 1)
                        notifyItemChanged(7, 1)
                    }

                    holder.itemView.zhengdian_item2.setOnClickListener {
                        if (holder.itemView.zhengdian_item2.isChecked) {
                            holder.itemView.zhengdian_item2.setTextColor(Color.WHITE)
                            if (position < 7) {
                                zhengdian_select[zhengdian_song[position][1]] = true
                            } else {
                                bandian_select[zhengdian_song[position][1]] = true
                            }
                        } else {
                            holder.itemView.zhengdian_item2.setTextColor(resources.getColor(R.color.gongkai_txt))
                            if (position < 7) {
                                zhengdian_select.remove(zhengdian_song[position][1])
                            } else {
                                bandian_select.remove(zhengdian_song[position][1])
                            }
                        }
                        notifyItemChanged(position, 1)
                        notifyItemChanged(0, 1)
                        notifyItemChanged(7, 1)
                    }

                    holder.itemView.zhengdian_item3.setOnClickListener {
                        if (holder.itemView.zhengdian_item3.isChecked) {
                            holder.itemView.zhengdian_item3.setTextColor(Color.WHITE)
                            if (position < 7) {
                                zhengdian_select[zhengdian_song[position][2]] = true
                            } else {
                                bandian_select[zhengdian_song[position][2]] = true
                            }
                        } else {
                            holder.itemView.zhengdian_item3.setTextColor(resources.getColor(R.color.gongkai_txt))
                            if (position < 7) {
                                zhengdian_select.remove(zhengdian_song[position][2])
                            } else {
                                bandian_select.remove(zhengdian_song[position][2])
                            }
                        }
                        notifyItemChanged(position, 1)
                        notifyItemChanged(0, 1)
                        notifyItemChanged(7, 1)
                    }

                    holder.itemView.zhengdian_item1.setOnLongClickListener {
                        if (position < 7) {
                            alarmSong(zhengdian_song[position][0], this@RepeatZhengDianActivity)
                        } else {
                            alarmSong(zhengdian_song[position][0], this@RepeatZhengDianActivity)
                        }
                        true
                    }
                    holder.itemView.zhengdian_item2.setOnLongClickListener {
                        if (position < 7) {
                            alarmSong(zhengdian_song[position][1], this@RepeatZhengDianActivity)
                        } else {
                            alarmSong(zhengdian_song[position][1], this@RepeatZhengDianActivity)
                        }
                        true
                    }
                    holder.itemView.zhengdian_item3.setOnLongClickListener {
                        if (position < 7) {
                            alarmSong(zhengdian_song[position][2], this@RepeatZhengDianActivity)
                        } else {
                            alarmSong(zhengdian_song[position][2], this@RepeatZhengDianActivity)
                        }
                        true
                    }

                }
            }

            if (holder is HeaderHolder) {
                if (position == 0) {
                    holder.itemView.zd_header.text = resources.getString(R.string.zhengdian_tx).format(zhengdian_select.size)
                }
                if (position == 7) {
                    holder.itemView.zd_header.text = resources.getString(R.string.bandian_tx).format(bandian_select.size)
                }
            }
        }

        override fun getItemCount() = zhengdian_data.size

        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
            val headerView = LayoutInflater.from(this@RepeatZhengDianActivity).inflate(R.layout.repeat_zhengdian_header, parent, false)
            val itemView = LayoutInflater.from(this@RepeatZhengDianActivity).inflate(R.layout.repeat_zhengdian_item, parent, false)
            if (viewType == ITEM_TYPE.HEADER.ordinal) {
                return HeaderHolder(headerView)
            }
            return ItemHolder(itemView)
        }

        override fun getItemViewType(position: Int): Int {
            if (position == 0 || position == 7) {
                return ITEM_TYPE.HEADER.ordinal
            }
            return ITEM_TYPE.ITEM.ordinal
        }

        inner class HeaderHolder(itemview: View) : RecyclerView.ViewHolder(itemview)
        inner class ItemHolder(itemview: View) : RecyclerView.ViewHolder(itemview)

    }


    private fun alarmSong(song: String, context: Context) {
        try {
            var vibrator = context.getSystemService(Context.VIBRATOR_SERVICE) as Vibrator
            val pattern = longArrayOf(100, 400) // 停止 开启 停止 开启
            vibrator.vibrate(pattern, -1) // 重复两次上面的pattern

            var fileDescriptor: AssetFileDescriptor = context.applicationContext.assets.openFd(song + ".mp3")
            var mediaPlayer = MediaPlayer()
            mediaPlayer.setDataSource(fileDescriptor.fileDescriptor, fileDescriptor.startOffset, fileDescriptor.length)
            mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC)
            mediaPlayer.prepare()
            mediaPlayer.start()
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }
}